<template>
  <div>
    <el-form size="small" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px">
      <el-form-item label="帐号" prop="account">
        <el-input v-model="ruleForm.account" clearable></el-input>
      </el-form-item>
      <el-form-item label="用户名" prop="name">
        <el-input v-model="ruleForm.name" clearable></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="ruleForm.password" clearable></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobilePhone">
        <el-input v-model="ruleForm.mobilePhone" clearable></el-input>
      </el-form-item>
    </el-form>
    <div style="text-align: center;">
      <el-button @click="$emit('changeAddUserFlag', false)">取 消</el-button>
      <el-button type="primary" @click="toSubmit">确 定</el-button>
    </div>
  </div>
</template>

<script>
import * as userApi from "@/api/userApi";
export default {
  data() {
    return {
      ruleForm: {},
      rules: {
        name: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        account: [{ required: true, message: "请输入帐号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        mobilePhone: [
          {
            required: true,
            min: 11,
            max: 11,
            message: "请输入正确的手机号",
            trigger: "blur"
          }
        ]
      }
    };
  },
  props: {
    addUserFlag: Boolean
  },
  watch: {
    addUserFlag(value) {
      if (value) {
        this.$refs.ruleForm.resetFields();
      }
    }
  },
  methods: {
    toSubmit() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          userApi.addUser(this.ruleForm).then(res => {
            if (res.errorCode == 0) {
              this.$message({
                showClose: true,
                message: "添加成功！",
                type: "success"
              });
              this.$emit("changeAddUserFlag", false);
            }
          });

        }
      });
    }
  }
};
</script>

<style lang="less" scope>
</style>
